module.exports = function ($scope, $modal, $resource, typelist, devicelist, add, del, 
	toaster, $state, $stateParams,slist, editurl) {

	$scope.searchform = {};
	if($stateParams.placecode){
		$scope.showTitle = false;
		$scope.searchform.view = $stateParams.placecode;
	} else {
		$scope.showTitle = true;
	}
	
    $scope.bigCurrentPage = 1;      //当前页码
    $scope.itemsPerPage = 10;         //每页显示几条
	$scope.addview = {'name' : '-----全部----', 'value' : ''};

	//景区下拉
	slist().then(function (res) {
		if (res.errcode === 0) {
			$scope.viewarr = res.data;
			$scope.viewarr.splice(0,0,$scope.addview);
		} else {
			alert(res.errmsg);
		}
	});

	$scope.load = function() {
		var para = {
			pageNo:$scope.bigCurrentPage, 
			pageSize:$scope.itemsPerPage
		};
	   	para = angular.extend($scope.searchform, para); 
		devicelist.save(para, function (res) {
			if (res.errcode === 0) {
				$scope.objs = res.data.results;
				$scope.bigTotalItems = res.data.totalRecord;
			} else {
				alert(res.errmsg);
			}
		});
	}
	$scope.load();

	$scope.myKeyup = function (e) {
		//IE 编码包含在window.event.keyCode中，Firefox或Safari 包含在event.which中
		var keycode = window.event ? e.keyCode : e.which;
		if (keycode == 13) {
			$scope.load();
		}
	};


	//打开模态框
	$scope.configurationticket = function (device_code, place_code) {
    var typelist1 = typelist;
    var add1 = add;
    var del1 = del;
		var modalInstance = $modal.open({
			template: require('../views/configureTicketType.html'),
			controller: 'deviceConfigureTicketType',
			size: 'lg',
			resolve: {
				view: function () {
					return place_code;
				},
				device_code: function () {
					return device_code;
				},
				'typelist': function () {
					return typelist1;
				},
				'add': function () {
					return add1;
				},
				'del': function () {
					return del1;
				}
			}
		});
		modalInstance.result.then(function () {
			$scope.load();
		});
	}

	$scope.edit = function (id) {
		$state.go(editurl, { 'id': id });
	};

	$scope.create = function () {
		$state.go('app.deviceCreate', {});
	}

	$scope.addDevice = function(){
		$resource('/api/ac/tc/ticketDeviceService/createWxDeviceCheckCode', {}, {}).save({'view': $scope.searchform.view}, function(res){
			if (res.errcode === 0){
				var modalInstance = $modal.open({
					template: require('../views/tourQrcode.html'),
					controller: 'tourQrcode',
					size: 'sm',
					resolve: {
						qrcode: function () {
							return res.data.result.url;
						}
					}
				});
				modalInstance.result.then(function () {
					$scope.load();
				});
			} else {
				toaster.error({title: '', body: '生成二维码失败'});
			}
		});
		
	}

	$scope.setStatus = function(obj, flag){
		var para = {'id': obj.id },msg = '';
		if(flag){
			msg = obj.state == 1 ? '开启设置，此设备即可验票' : '停用后，此设备无法继续验票；请确认后再执行此操作！';
			para.state = obj.state;
		} else {
			msg = obj.auth_state == 1 ? '开启散票权限吗?' : '停用散票权限，此设备无法验证散票';
			para.auth_state = obj.auth_state;
		}
		
		if(confirm(msg)){
			$resource('/api/as/tc/device/updateState', {}, {}).save(para, function (res) {
				if (res.errcode === 0) {
					toaster.success({title: '', body: '成功'});
				} else {
					toaster.error({title: '', body: '设置状态失败, 请重试'});
				}
			});
		} else {
			if(flag){
				obj.state = obj.state == 1 ? 0 : 1;
			} else {
				obj.auth_state = obj.auth_state == 1 ? 0 : 1;
			}
		}
		
		
	}

};